﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using ITSP.SharePoint.CacheRefresh.Examples.Configuration;
using Microsoft.SharePoint;

namespace ITSP.SharePoint.CacheRefresh.Examples.EventReceivers
{
    /// <summary>
    /// The List Event Receiver for the Example List Template.
    /// </summary>
    [Guid("574eca03-904a-4c69-b4b7-2542eb197441")]
    public class CacheExampleListEventReceiver : Microsoft.SharePoint.SPItemEventReceiver
    {
        

        /// <summary>
        /// 
        /// </summary>
        /// <param name="properties"></param>
        public override void ItemUpdated(Microsoft.SharePoint.SPItemEventProperties properties)
        {
            //get the SharePoint List Item
            SPListItem updatedItem = properties.ListItem;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite secureSite = new SPSite(properties.SiteId))
                    {
                        secureSite.CatchAccessDeniedException = false;

                        //update the configuration
                        ExampleConfigurationManager manager = ExampleConfigurationManager.Current;
                        if (manager != null)
                        {
                            manager.Refresh();
                            ExampleConfiguration configuration = manager.Configuration;
                            string listItemTitle = String.Empty;
                            string listItemValue = String.Empty;
                            if (updatedItem.Fields.ContainsField("Title"))
                            {
                                listItemTitle = updatedItem["Title"].ToString();
                            }
                            if (updatedItem.Fields.ContainsField("Subject"))
                            {
                                listItemValue = updatedItem["Subject"].ToString();
                            }

                            switch (listItemTitle)
                            {
                                case "ExampleConfigurationName":
                                    configuration.ExampleConfigurationName = listItemValue;
                                    break;
                                case "ExampleConfigurationValue":
                                    configuration.ExampleConfigurationValue = Convert.ToInt32(listItemValue);
                                    break;
                                case "ExampleConfigurationStartDate":
                                    configuration.ExampleConfigurationStartDate = DateTime.Parse(listItemValue);
                                    break;
                                default:
                                    properties.ErrorMessage = String.Format("List Item Title: {0} Not Supported",
                                                                            listItemTitle);
                                    properties.Cancel = true;
                                    break;
                            }

                            configuration.Modified = DateTime.Now;
                            manager.Save(configuration);

                        }
                    }
                });
            }
            catch (Exception ex)
            {
                
                throw new SPException("Error Updating the List", ex);
            }
            
            
            //base.ItemUpdated(properties);
        }
    }
}
